package main

import (
	"fmt"
	"io"
	"log"
	"strconv"
	"strings"
)

func ifElseTest() {
	age := 10
	if age < 10 {
		fmt.Println("age less than 10")
	} else {
		fmt.Println("age more than 10")
	}

	userInput := "10"
	if userInt, err := strconv.Atoi(userInput); err == nil {
		fmt.Printf("you input int value: %T, %d", userInt, userInt)
	}
}

func switchTest() {
	username := "go"
	switch username {
	case "byref", "go":
		fmt.Println("ok")
		fallthrough
		//break
	case "test":
		fmt.Println("failure")
	default:
		fmt.Println("default switch")
	}

	i := -1
	switch {
	case i < 0:
		fmt.Println("i < 0")
	case i == 0:
		fmt.Println("i=0")
	//case i < 0:
	//	fmt.Println("i<0 tow")
	case i > 0:
		fmt.Println("i > 0")

	}

	k := 6
	switch k {
	case 4:
		fmt.Println("was <= 4")
		fallthrough
	case 5:
		fmt.Println("was <= 5")
		fallthrough
	case 6:
		fmt.Println("was <= 6")
		fallthrough
	case 7:
		fmt.Println("was <= 7")
		fallthrough
	case 8:
		fmt.Println("was <= 8")
		fallthrough
	default:
		fmt.Println("default case")
	}
}

func forTest() {
	//for i := 1; i < 10; i++ {
	//	ms := time.Millisecond + time.Duration(i*1000*1000*1000)
	//	//ms := time.Millisecond * 2000
	//	fmt.Printf("start %d, now: %s\n", ms, time.Now())
	//	time.Sleep(ms)
	//	fmt.Printf("end now: %s\n", time.Now())
	//}

	//for i:=0; i<25; i++ {
	//	for j:=0; j<i+1; j++ {
	//		//fmt.Printf("%d %d\n", i, j)
	//		fmt.Printf("%s", "D")
	//	}
	//	fmt.Println()
	//}
	for i := 1; i <= 25; i++ {
		//gStr := strings.Repeat("G", i)
		//fmt.Println(gStr)
	}

	//for i := 1; i <= 100; i++ {
	//	switch {
	//	case i%3 == 0:
	//		fmt.Println("fizz", i)
	//	case i%5 == 0:
	//		fmt.Println("buzz", i)
	//	case i%5 == 0 && i%3 == 0:
	//		fmt.Println("FizzBuzz", i)
	//	}
	//}

	initValue := 10
	for initValue >= 0 {
		initValue--
		fmt.Println("initValue ->", initValue)
	}
}

func forRangeTest() {
	//str := "测试者"
	//for idx, s := range str {
	//	fmt.Printf("index: %d = %c\n", idx, s)
	//}
	for i := 0; i < 5; i++ {
		var v int
		fmt.Printf("%d ", v)
		//v = 5
	}

	s := ""
	for ; s != "aaaaa"; {
		fmt.Println("Value of s:", s)
		s = s + "a"
	}
}

func returnValueTest(x int, y int) (addVal int, multipleValue int) {
	addVal = x + y
	multipleValue = x * y
	return
}

func myFunc(prefix string, who ...string) {
	fmt.Println(prefix, "hi:", strings.Join(who, ";"))
}

func sliceTestMin(s ...int) int {
	if len(s) == 0 {
		return 0
	}
	min := s[0]
	for _, v := range s {
		if v < min {
			min = v
		}
	}
	return min
}

type Options struct {
	par1 int
	par2 int
}

func structTest(x int, y int, opt Options) {
	fmt.Printf("x=%d, y=%d \n", x, y)
	fmt.Println(opt)
}

func useEmptyInterface(values ...interface{}) {
	for _, v := range values {
		//type  = v.(type)
		fmt.Printf("%T, %v\n", v, v)
	}
}

func fun1RequireDefer() {
	fmt.Println("begin call fun1 require defer")
	defer deferFunc()
	fmt.Println("end call func1")

	i := 0
	defer fmt.Println(i)
	i = 1

	for j := 0; j < 5; j++ {
		defer fmt.Println("j=", j)
	}
}

func deferFunc() {
	fmt.Println("this call after func1 called")
}

func logFunc1(s string) (n int, err error) {
	defer func() {
		log.Printf("logFunc1(%q) = %d, %v", s, n, err)
	}()
	return 10, io.EOF
}
